package com.airsystem.pos.bengkel.service.converter;

import java.math.BigDecimal;

import com.airsystem.pos.bengkel.entity.DetailPesanSparepart;
import com.airsystem.pos.bengkel.entity.DetailPesanSparepart.Metode;
import com.airsystem.pos.bengkel.entity.PesanSparepart;
import com.airsystem.pos.bengkel.entity.wrapper.PesanSparepartWrapper;
import com.airsystem.pos.bengkel.interfaces.IParser;
import com.airsystem.pos.bengkel.util.DateUtil;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class SparepartEntityConverter implements IParser<PesanSparepartWrapper, PesanSparepart> {

	public PesanSparepartWrapper doParse(PesanSparepart data) {
		String tanggal = DateUtil.convertDateDisplay(data.getTanggal());

		int totalProduk = data.getDetail_pesan_sparepart().size();

		BigDecimal totalHarga = new BigDecimal(0);

		Metode prevMetode = Metode.UNKNOWN;

		for (DetailPesanSparepart item : data.getDetail_pesan_sparepart()) {
			totalHarga = totalHarga.add(item.getHarga().multiply(BigDecimal.valueOf(item.getStok())));

			if (prevMetode == Metode.UNKNOWN) {
				prevMetode = item.getMetode_e();
			} else if ((prevMetode == Metode.TUNAI && item.getMetode_e() == Metode.HUTANG)
				   ||  (prevMetode == Metode.HUTANG && item.getMetode_e() == Metode.TUNAI)) {
				prevMetode = Metode.BOTH;
			}
		}

		return new PesanSparepartWrapper(data, tanggal, totalProduk, totalHarga, prevMetode);
	}
}